package cn.laixueit.service;

import cn.laixueit.mapper.UserMapper;
import cn.laixueit.pojo.user.User;
import cn.laixueit.utils.CodecUtils;
import cn.laixueit.utils.NumberUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Boolean check(String data, Integer type) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        switch (type){
            case 1:
                wrapper.eq("username",data);
                break;
            case 2:
                wrapper.eq("phone",data);
                break;
            default:
                return null;
        }
        int count = count(wrapper);
        return count == 0;
    }

    @Override
    public Boolean sendCode(String phone) {
        if(!StringUtils.isNotBlank(phone)){
            return false;
        }
        // 生成随机验证码
        String code = NumberUtils.generateCode(6);
        // 存入rabbitmq
        HashMap<String, String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("chuangmei.sms.exchange","sms.code",map);
        // 存入redis
        redisTemplate.opsForValue().set("user:code:phone:"+phone,code,5, TimeUnit.MINUTES);
        return true;
    }

    @Override
    public Boolean register(User user, String code) {
        String redisCode = redisTemplate.opsForValue().get("user:code:phone:" + user.getPhone());
        if(!StringUtils.equals(redisCode,code)){
            return  false;
        }

        // 生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));

        user.setId(null);
        user.setCreated(new Date());

        boolean b = save(user);
        if(b){
            redisTemplate.delete("user:code:phone:" + user.getPhone());
        }
        return b;
    }

    @Override
    public User queryUser(String username, String password) {
        if(username == null || password == null){
            return  null;
        }

        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        User user = getOne(wrapper);

        if (!CodecUtils.md5Hex(password,user.getSalt()).equals(user.getPassword())) {
            return null;
        }

        return user;
    }
}
